package com.czy.daishusys.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.czy.daishusys.R;
import com.czy.daishusys.entity.DeliverGoodsModel;
import com.czy.daishusys.ui.order.DeliverAddressActivity;
import com.czy.daishusys.ui.order.DeliverGoodsEvent;
import com.czy.daishusys.utils.PhoneUtils;
import com.czy.daishusys.widget.MyRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/12/4.
 */

public class DeliverGoodsAdapter extends BaseQuickAdapter<DeliverGoodsModel.DataBean.UListBean, BaseViewHolder> {
    public DeliverGoodsAdapter(int layoutResId, @Nullable List<DeliverGoodsModel.DataBean.UListBean> data) {
        super(layoutResId, data);
    }

    private TextView tvLogisticsMode;

    @Override
    protected void convert(final BaseViewHolder holder, final DeliverGoodsModel.DataBean.UListBean item) {
        MyRecyclerView rvDeliverGoods = (MyRecyclerView) holder.getView(R.id.rv_deliver_goods);
        TextView tvDeliverAddress = (TextView) holder.getView(R.id.tv_deliver_address);
        LinearLayout llDeliverAddress = (LinearLayout) holder.getView(R.id.ll_deliver_address);
        tvLogisticsMode = (TextView) holder.getView(R.id.tv_logistics_mode);
        LinearLayout llLogisticsMode = (LinearLayout) holder.getView(R.id.ll_logistics_mode);
        EditText etLogisticsContact = (EditText) holder.getView(R.id.et_logistics_contact);
        EditText etLogisticsNumber = (EditText) holder.getView(R.id.et_logistics_number);
        EditText etLogisticsName = (EditText) holder.getView(R.id.et_logistics_name);

        //地址
        List<DeliverGoodsModel.DataBean.UListBean.AddressListBean> addressList = item.getAddressList();
        if (addressList != null && addressList.size() > 0) {
            for (int i = 0; i < addressList.size(); i++) {
                if (addressList.get(i).isAddressChecked()) {
                    DeliverGoodsModel.DataBean.UListBean.AddressListBean addressListBean = addressList.get(i);
                    tvDeliverAddress.setText(addressListBean.getProvinceName() + addressListBean.getCityName() + addressListBean.getAreaName() + addressListBean.getDetailLocation());
                }
            }

        }
        //运费模板
        List<DeliverGoodsModel.DataBean.UListBean.DeliveryWayListBean> deliveryWayList =
                item.getDeliveryWayList();
        if (deliveryWayList != null && deliveryWayList.size() > 0) {
            for (int i = 0; i < deliveryWayList.size(); i++) {
                if (deliveryWayList.get(i).isDeliverWayCheck()) {
                    tvLogisticsMode.setText(deliveryWayList.get(i).getName());
                }
            }
        }

        llDeliverAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, DeliverAddressActivity.class);
                intent.putExtra("addressList", (Serializable) item.getAddressList());
                intent.putExtra("position", holder.getAdapterPosition());
                mContext.startActivity(intent);
            }
        });
        //运费模板选择
        llLogisticsMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> optionsDeliver = new ArrayList<>();

                if (item.getDeliveryWayList() == null || item.getDeliveryWayList().size() == 0) {
                    PhoneUtils.ShowToastMessage(mContext, "无运费模板!");
                } else {
                    for (int i = 0; i < item.getDeliveryWayList().size(); i++) {
                        optionsDeliver.add(item.getDeliveryWayList().get(i).getName());
                    }
                    OptionsPickerView pvDeliverOptions = new OptionsPickerView(mContext);
                    pvDeliverOptions.setPicker(optionsDeliver);
                    pvDeliverOptions.setTitle("");
                    pvDeliverOptions.setCyclic(false);
                    pvDeliverOptions.setSelectOptions(0);
                    pvDeliverOptions.setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int option1, int option2, int option3) {
                            tvLogisticsMode.setText(item.getDeliveryWayList().get(option1).getName());
                            EventBus.getDefault().post(new DeliverGoodsEvent("logistics", holder.getAdapterPosition(), option1, "", false));
                        }
                    });
                    pvDeliverOptions.show();
                }


            }
        });
        DeliverGoodsItemAdapter deliverGoodsAdapter = new DeliverGoodsItemAdapter(R.layout.item_deliver_goods_sub_recycler, item.getOrderItems());
        rvDeliverGoods.setLayoutManager(new LinearLayoutManager(mContext));
        rvDeliverGoods.setAdapter(deliverGoodsAdapter);
        rvDeliverGoods.setNestedScrollingEnabled(false);

        if (etLogisticsContact.getTag() instanceof TextWatcher) {
            etLogisticsContact.removeTextChangedListener((TextWatcher) (etLogisticsContact.getTag()));
        }
        if (!TextUtils.isEmpty(item.getLogisticsContact())) {
            etLogisticsContact.setText(item.getLogisticsContact() + "");
        }

        TextWatcher contactWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int parentPosition = holder.getAdapterPosition()+getFooterLayoutCount()+getHeaderLayoutCount();
                if (TextUtils.isEmpty(editable.toString())) {
                    EventBus.getDefault().post(new DeliverGoodsEvent("contact", parentPosition, 0, "", false));
                } else {
                    EventBus.getDefault().post(new DeliverGoodsEvent("contact", parentPosition, 0, editable.toString(), false));
                }

            }
        };
        etLogisticsContact.addTextChangedListener(contactWatcher);
        etLogisticsContact.setTag(contactWatcher);


        if (etLogisticsNumber.getTag() instanceof TextWatcher) {
            etLogisticsNumber.removeTextChangedListener((TextWatcher) (etLogisticsNumber.getTag()));
        }
        if (!TextUtils.isEmpty(item.getLogisticsNumber())) {
            etLogisticsNumber.setText(item.getLogisticsNumber() + "");
        }

        TextWatcher numberWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    EventBus.getDefault().post(new DeliverGoodsEvent("number", holder.getAdapterPosition(), 0, "", false));
                } else {
                    EventBus.getDefault().post(new DeliverGoodsEvent("number", holder.getAdapterPosition(), 0, editable.toString(), false));
                }

            }
        };
        etLogisticsNumber.addTextChangedListener(numberWatcher);
        etLogisticsNumber.setTag(numberWatcher);

        if (etLogisticsName.getTag() instanceof TextWatcher) {
            etLogisticsName.removeTextChangedListener((TextWatcher) (etLogisticsName.getTag()));
        }
        if (!TextUtils.isEmpty(item.getLogisticsName())) {
            etLogisticsName.setText(item.getLogisticsName() + "");
        }

        TextWatcher nameWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    EventBus.getDefault().post(new DeliverGoodsEvent("name", holder.getAdapterPosition(), 0, "", false));
                } else {
                    EventBus.getDefault().post(new DeliverGoodsEvent("name", holder.getAdapterPosition(), 0, editable.toString(), false));
                }

            }
        };
        etLogisticsName.addTextChangedListener(nameWatcher);
        etLogisticsName.setTag(nameWatcher);

    }
}
